package com.neouton.common.util;

import com.neouton.crm.entity.SysUserEntity;
import com.neouton.email.EmailType;
import com.neouton.email.entity.EmailVO;
import com.neouton.email.feginService.SentMailFeginService;
import com.neouton.feginClient.FeginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class EmailUtils {

    @Resource
    SentMailFeginService sentMailFeginService;

    @Resource
    FeginService feginService;

    private static SentMailFeginService staticSentMailFeginService;

    private static FeginService staticFeginService;


    @PostConstruct
    public void init() {
        staticSentMailFeginService = sentMailFeginService;
        staticFeginService = feginService;
    }

    /**
     * 通过用户ID发送邮件
     * @param addresseeUserIds 收件人用户ID集合
     * @param ccUserIds 抄送人ID集合
     * @param title 邮件主题
     * @param msg 邮件内容
     * @param token token
     */
    public static String sentMailByUserId(List<Integer> addresseeUserIds,List<Integer> ccUserIds, String title, String msg, String token){
        if((addresseeUserIds == null || addresseeUserIds.isEmpty()
        || StringUtils.isBlank(title) || StringUtils.isBlank(msg))){
            return "关键参数不能为空！";
        }
        StringBuffer addresseeUserEmails = new StringBuffer();
        for (Integer userId:addresseeUserIds){
            ResponseAPI<SysUserEntity> responseAPI = staticFeginService.searchUserInfoByIds(userId,token);
            if(addresseeUserEmails.isEmpty()){
                addresseeUserEmails.append(responseAPI.getResult().getEmail());
            }else {
                addresseeUserEmails.append("," + responseAPI.getResult().getEmail());
            }
        }

        StringBuffer ccUserEmails = new StringBuffer();
        if((ccUserIds != null && !ccUserIds.isEmpty())){
            for (Integer userId:ccUserIds){
                ResponseAPI<SysUserEntity> responseAPI = staticFeginService.searchUserInfoByIds(userId,token);
                if(ccUserEmails.isEmpty()){
                    ccUserEmails.append(responseAPI.getResult().getEmail());
                }else {
                    ccUserEmails.append("," + responseAPI.getResult().getEmail());
                }
            }
        }

        EmailVO emailVO = new EmailVO();
        //收件人
        emailVO.setAddressee(addresseeUserEmails.toString());
        //抄送人
        emailVO.setCcUser(ccUserEmails.toString());
        //邮件主题
        emailVO.setSubject(title);
        //邮件内容
        emailVO.setMailContent(msg);
        //邮件格式 HTML
        emailVO.setEmtilType(EmailType.EMAIL_TYPE_HTML);
        staticSentMailFeginService.sentMail(emailVO);
        return "true";
    }

    /**
     * 通过用户邮箱
     * @param addresseeUserEmails 收件人用户ID集合 多邮箱以,分隔
     * @param ccUserEmails 抄送人ID集合 多邮箱以,分隔
     * @param title 邮件主题
     * @param msg 邮件内容
     * @param token token
     */
    public static String sentMailByAddressee(String addresseeUserEmails,String ccUserEmails, String title, String msg, String token){
        if((addresseeUserEmails == null || addresseeUserEmails.isEmpty()
                || StringUtils.isBlank(title) || StringUtils.isBlank(msg))){
            return "关键参数不能为空！";
        }

        EmailVO emailVO = new EmailVO();
        //收件人
        emailVO.setAddressee(addresseeUserEmails);
        //抄送人
        emailVO.setCcUser(ccUserEmails);
        //邮件主题
        emailVO.setSubject(title);
        //邮件内容
        emailVO.setMailContent(msg);
        //邮件格式 HTML
        emailVO.setEmtilType(EmailType.EMAIL_TYPE_HTML);
//        staticSentMailFeginService.sentMail(emailVO);
        return "true";
    }

    /**
     * 通过用户邮箱
     * @param addresseeUserEmails 收件人用户ID集合 多邮箱以,分隔
     * @param ccUserEmails 抄送人ID集合 多邮箱以,分隔
     * @param title 邮件主题
     * @param msg 邮件内容
     * @param token token
     */
    public static String sentMailByAddresseeForData(String addresseeUserEmails,String ccUserEmails, String title, String msg, String token){
        if((addresseeUserEmails == null || addresseeUserEmails.isEmpty()
                || StringUtils.isBlank(title) || StringUtils.isBlank(msg))){
            return "关键参数不能为空！";
        }

        EmailVO emailVO = new EmailVO();
        //收件人
        emailVO.setAddressee(addresseeUserEmails);
        //抄送人
        emailVO.setCcUser(ccUserEmails);
        //邮件主题
        emailVO.setSubject(title);
        //邮件内容
        emailVO.setMailContent(msg);
        //邮件格式 HTML
        emailVO.setEmtilType(EmailType.EMAIL_TYPE_HTML);
        staticSentMailFeginService.sentMail(emailVO);
        return "true";
    }

}
